<!DOCTYPE html>
<title>Service Worker: UseCounter for page controlled-on-reload</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script src="resources/usecounter.js"></script>
<script>

promise_test(function(t) {
    var url = 'resources/empty-worker.js';
    var scope = 'resources/blank.html?on-reload';
    var frame;

    return service_worker_unregister(t, scope)
      .then(function() { return with_iframe(scope); })
      .then(function(f) {
          frame = f;
          add_completion_callback(function() { frame.remove(); });
          var w = frame.contentWindow;
          assert_equals(w.navigator.serviceWorker.controller, null);
          assert_false(isUseCounted(frame),
                       'should not be counted as controlled yet');
          return navigator.serviceWorker.register(url, {scope: scope});
        })
      .then(function(registration) {
          add_completion_callback(function() { registration.unregister(); });
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          var w = frame.contentWindow;
          assert_equals(w.navigator.serviceWorker.controller, null);
          return new Promise(function(resolve) {
              frame.onload = function() { resolve(); }
              w.location.reload();
            });
        })
      .then(function() {
          var w = frame.contentWindow;
          var controller = w.navigator.serviceWorker.controller;
          assert_true(controller instanceof w.ServiceWorker);
          assert_true(isUseCounted(frame),
                      'should be counted as controlled after reload()');
        });
  }, 'usecounter is counted upon reload after registration');

</script>
</html>
